
Ext.define('Avrio.view.Person', {
    extend: 'Ext.grid.Panel',

    alias: 'widget.person',

    title: 'Person Table',
    store: 'Person',

    frame: true,
    columnLines: true,
    selType: 'cellmodel',

    initComponent: function() {
        var me = this; // me == grid

        me.rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
            clicksToMoveEditor: 1,
            autoCancel: false
        });
        Ext.applyIf(me, {
            height: 500,
            plugins: [me.rowEditing], //me.editing, me.rowEditing
            viewConfig: { stripeRows: true },
//<editor-fold defaultstate="collapsed" desc="columns">
            columns: [
                { xtype: 'rownumberer' },
                //{ xtype: 'gridcolumn', header: 'ID', dataIndex: 'id', width: 100 },
                {
                    xtype: 'gridcolumn', header: 'Email', dataIndex: 'email', width: 150, align: 'right', sortable: true, menuDisabled: false,
                    editor: { allowBlank: false, vtype: 'email' }
                }, {
                    xtype: 'gridcolumn', header: 'Version', dataIndex: 'version', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield', allowBlank: false }
                }, {
                    xtype: 'gridcolumn', header: 'License', dataIndex: 'drivers_license_num', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'SSN', dataIndex: 'ssn', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield', allowBlank: false }
                }, {
                    xtype: 'gridcolumn', header: 'Account', dataIndex: 'account_number', width: 90, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Name Prefix', dataIndex: 'name_prefix', width: 100, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Given Name', dataIndex: 'given_name', width: 100, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Middle Name', dataIndex: 'middle_name', width: 100, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Family Name', dataIndex: 'family_name', width: 100, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Name Suffix', dataIndex: 'name_suffix', width: 100, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Alias Name', dataIndex: 'alias_name', width: 100, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Description', dataIndex: 'description', width: 150, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Telephone 1', dataIndex: 'telephone1', width: 90, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Telephone 2', dataIndex: 'telephone2', width: 90, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Telephone 3', dataIndex: 'telephone3', width: 90, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Gender', dataIndex: 'gender_code', width: 50, align: 'center', sortable: false, menuDisabled: true,
                    editor: { xtype: 'combobox', store: ['M','F','UN'], queryMode:'local', autoSelect: true, forceSelection: true }
                }, {
                    xtype: 'datecolumn', header: 'Birth', dataIndex: 'birth_time', renderer: Ext.util.Format.dateRenderer('m/d/Y'), width: 95, align: 'right', sortable: false, menuDisabled: true,
                    editor: { 
                        xtype: 'datefield',
                        format: 'm/d/y',
                        //allowBlank: false,
                        minValue: '01/01/1901',
                        maxValue: Ext.Date.format(new Date(), 'm/d/Y')
                        //minText: 'Cannot have a start date before!',
                        //maxText: 'The date in this field must be equal to or before {date}'
                    }
                }, {
                    xtype: 'checkcolumn', header: 'Multiple', dataIndex: 'multiple_birth_ind', width: 50, align: 'center', sortable: false, menuDisabled: true,
                    editor: { xtype: 'checkbox' }
                }, {
                    xtype: 'gridcolumn', header: 'Birth Order', dataIndex: 'birth_order', width: 80, align: 'right', sortable: false, menuDisabled: true,
                    editor: { xtype: 'numberfield', minValue: 0 }
                }, {
                    xtype: 'checkcolumn', header: 'Deceased', dataIndex: 'deceased_ind', width: 50, align: 'center', sortable: false, menuDisabled: true,
                    editor: { xtype: 'checkbox' }
                }, {
                    xtype: 'datecolumn', header: 'Deceased Date', dataIndex: 'deceased_datetime', renderer: Ext.util.Format.dateRenderer('m/d/Y'), width: 95, align: 'right', sortable: false, menuDisabled: true,
                    editor: {
                        xtype: 'datefield',
                        format: 'm/d/y',
                        //allowBlank: false,
                        minValue: '01/01/1901',
                        maxValue: Ext.Date.format(new Date(), 'm/d/Y')
                    }
                }, {
                    xtype: 'checkcolumn', header: 'Organ Doner', dataIndex: 'organ_doner_ind', width: 90, align: 'center', sortable: false, menuDisabled: true,
                    editor: { xtype: 'checkbox' }
                }, {
                    xtype: 'gridcolumn', header: 'Address 1', dataIndex: 'address_line1', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Address 2', dataIndex: 'address_line2', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'City', dataIndex: 'city', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, { 
                    xtype: 'gridcolumn', header: 'State', dataIndex: 'state_code', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Postal', dataIndex: 'postal_code', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Country', dataIndex: 'country_code', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Marital Status', dataIndex: 'marital_status_code', width: 90, align: 'center', sortable: false, menuDisabled: true,
                    editor: { xtype: 'combobox', store: ['A','D','I','L','M','P','S','T','W'], queryMode:'local', autoSelect: true, forceSelection: true }
                    //ENUM(' ','A','D','I','L','M','P','S','T','W') COMMENT 'A(Annulled), D(Divorced), I(Interlocutory), L(Legally Separated), M(Married), P(Polygamous), S(Never Married), T(Domestic partner), W(Widowed)',
                }, {
                    xtype: 'gridcolumn', header: 'Occupation', dataIndex: 'occupation_code', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Nationality', dataIndex: 'nationality', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Citizen', dataIndex: 'citizen_role_type', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Related Person', dataIndex: 'related_person', width: 89, align: 'center', sortable: false, menuDisabled: true
                }, {
                    xtype: 'gridcolumn', header: 'Birth Place', dataIndex: 'birth_place', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Language', dataIndex: 'primary_language_code', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Race', dataIndex: 'race', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Ethnicity', dataIndex: 'ethnicity', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Education Level', dataIndex: 'education_level_code', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Disability Type', dataIndex: 'disability_type_code', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Living Arrangement', dataIndex: 'living_arrangement_code', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Religious Affiliation', dataIndex: 'religious_affiliation_code', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Mothers Maiden', dataIndex: 'mothers_maiden_name', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }, {
                    xtype: 'gridcolumn', header: 'Veterans Status', dataIndex: 'veterans_status', width: 99, align: 'right', sortable: false, menuDisabled: true,
                    editor: { }
                }
            ],
// </editor-fold>
            dockedItems: [{
                xtype: 'pagingtoolbar',
                store: 'Person',
                dock: 'bottom',
                displayInfo: true,
                items: [' ',{
                    text: 'Add Person',
                    handler: function() {
                        me.rowEditing.cancelEdit();

                        var rec = Ext.create('Avrio.model.Person',{
                            email: 'changeIt@test.com',
                            birth_time: new Date(),
                            deceased_datetime: new Date()
                        });

                        var store = me.getView().getStore();
                        store.insert(0, rec);
                        
                        me.rowEditing.startEdit(0, 0);
                    }
                }, {
                    text: 'Remove Person',
                    itemId: 'removePerson',
                    handler : function() {
                        me.rowEditing.cancelEdit();

                        var store = me.getView().getStore();
                        var selection = me.getView().getSelectionModel().getSelection()[0];
                        if(selection) {
                            store.remove(selection);
                        }
                    },
                    disabled: true
                }]
            }],
            listeners: {
                render: function(grid) {
                    grid.getStore().load();
                },
                selectionchange: function(view, records) {
                    me.down('#removePerson').setDisabled(!records.length);
                }
            }
        });
        me.callParent(arguments);
    }
});
